package com.shareware.common.utils;

import java.security.MessageDigest;

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */
public class SecretUtil {

    public static String encryptMD5(byte[] data) {
        return shaEncrypt(data, "MD5");
    }

    public static String encryptSHA1(byte[] data) {
        return shaEncrypt(data, "SHA-1");
    }

    public static String encryptSHA256(byte[] data) {
        return shaEncrypt(data, "SHA-256");
    }

    public static String shaEncrypt(byte[] data, String algorithmName) {
        if (data != null && data.length > 0) {
            try {
                MessageDigest messageDigest = MessageDigest.getInstance(algorithmName);
                messageDigest.update(data);
                return StringExtKt.toHexString(messageDigest.digest());
            } catch (Exception ignore) {
            }
        }
        return "";
    }

    public static native String md5(byte[] content);

    /**
     * 最好加上Base64加密，防止数据转义问题出现
     */
    public static native String encryptAES(String content);

    public static native String decryptAES(String cipherHex);

    //为了减小包体积，未开启openssl的BIO模块，故以下方法不可用

//    public static native byte[] encryptRSAPublic(byte[] content);

//    public static native byte[] decryptRSAPublic(byte[] cipherText);

//    /**
//     * 签名的目的是保证数据的一致性，不会被篡改
//     */
//    public static native byte[] rsaSignPrivate(byte[] context);

//    public static native boolean rsaVerifyPublic(byte[] cipherText, byte[] signText);

    static {
        System.loadLibrary("shareware");
    }
}
